package persist

import (
	"01-crawler-project/02/entity"
	"github.com/OwnLocal/goes"
	"log"
	"net/url"
)

var client *goes.Client

var host = "http://localhost:9200/"

var (
	ES_HOST = "localhost"
	ES_PORT = "9200"
)

//初始化
func init() {

	var err error

	client = goes.NewClient(ES_HOST, ES_PORT)

	if err != nil {

		print(err)
	}

}

func ItemServer() (chan interface{}) {

	ret := make(chan interface{})

	go func() {
		itemCount := 0
		for true {

			item := <-ret

			itemCount++

			switch item.(type) {

			case entity.UserInfo:

				userinfo := item.(entity.UserInfo)

				extraArgs := make(url.Values)

				d := goes.Document{
					Index:  "zhenai",
					Type:   "userinfo",
					Fields: userinfo,
					ID:     userinfo.Id,
				}

				_, e := client.Index(d, extraArgs)

				if e != nil {

					log.Println(" client.Index : ", e)
				}

			}

			log.Printf(" itemCount is : %d \n", itemCount)

		}

	}()

	return ret
}
